'use strict';//电影详情
const Controller = require('egg').Controller;
class movie_detailsController extends Controller {
    async index() { //模糊搜索跟分页查询
        const { ctx, app } = this;
        const { keyname, page = 1, number = 10 } = ctx.request.query
        const offset = (page - 1) * number
        let data, count
        if (keyname && page) {
            data = await app.mysql.query(`select id, movie_name,director,to_star,screenwriter,time_and_source,Issuing_company,more_titles,score,img,plot,movie_page from movie_details where movie_name like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from movie_details where movie_name like '%${keyname}%'`)
        } else if (keyname) {
            data = await app.mysql.query(`select id, movie_name,director,to_star,screenwriter,time_and_source,Issuing_company,more_titles,score,img,plot,movie_page from movie_details where movie_name ='%${keyname}%' or movie_name  like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from movie_name where movie_name like '%${keyname}%' or id like '%${keyname}%'`)
        } else {
            data = await app.mysql.query(`select id, movie_name,director,to_star,screenwriter,time_and_source,Issuing_company,more_titles,score,img,plot,movie_page from movie_details limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from movie_details`)
        }
        ctx.body = {
            code: 0,
            msg: "查找成功",
            data,
            count: count
        }
    }
    async create() {
        const { ctx, app } = this;
        const { movie_name, director, to_star, screenwriter, time_and_source, Issuing_company, more_titles, score, img, plot, movie_page } = ctx.request.body;
        const result = await app.mysql.insert('movie_details', { movie_name, director, to_star, screenwriter, time_and_source, Issuing_company, more_titles, score, img, plot, movie_page })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "添加成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 406,
                msg: "添加失败"
            };
        }
    }
    async update() {
        const { ctx, app } = this;
        const { movie_name, director, to_star, screenwriter, time_and_source, Issuing_company, more_titles, score, img, plot, movie_page } = ctx.request.body;
        const { id } = ctx.params;
        const row = {
            movie_name,
            director,
            to_star,
            screenwriter,
            time_and_source,
            Issuing_company,
            more_titles,
            score,
            img,
            plot,
            movie_page


        }
        const options = {
            where: {
                id: +id
            }
        };
        const result = await app.mysql.update('movie_details', row, options)
        if (movie_name, director, to_star, screenwriter, time_and_source, Issuing_company, more_titles, score, img, plot, movie_page) {
            ctx.body = {
                code: 0,
                msg: "修改成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 408,
                msg: "修改失败",
            };
        }
    }

    async destroy() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        const result = await app.mysql.delete(`movie_details`, { id: +id })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "删除成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 407,
                msg: "删除失败"
            };
        }
    }
}
module.exports = movie_detailsController;
